package de.hs_heilbronn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class TableBks {

	private ArrayList<Bk> bkList = new ArrayList<Bk>();
	private ArrayList<String> allbooks = new ArrayList<String>();
	private ArrayList<Stud> matrikelList = new ArrayList<Stud>();
	private String[] matrikelArray;
	


	public String[] getMatrikelArray() {
		return matrikelArray;
	}

	public void setMatrikelArray(String[] matrikelArray) {
		this.matrikelArray = matrikelArray;
	}

	// retrieves all book-objects from database and puts them into an ArrayList
	public TableBks() {
		Database db = new Database();
		ResultSet rs = db.connectQuery("SELECT * FROM buch;");
		ResultSet rs2 = db.connectQuery("SELECT * FROM student;");
		
		try {
			while (rs.next()) {
				bkList.add(new Bk(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), Integer.toString(rs.getInt(5))));
			}
			
			while (rs2.next()) {
				matrikelList.add(new Stud(rs2.getInt(1), rs2.getString(2), rs2.getString(3)));
			}
			
			matrikelArray = new String[matrikelList.size()+1];
			matrikelArray[0] = "nicht entliehen";
			
			for (int i=1; i<(matrikelList.size()+1); i++){
				matrikelArray[i] = "" + matrikelList.get(i-1).getMatrikel();
			}
			

			
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public ArrayList<Bk> getBkList() {
		return bkList;
	}

	public void setBkList(ArrayList<Bk> bkList) {
		this.bkList = bkList;
	}

	public ArrayList<Stud> getMatrikelList() {
		return matrikelList;
	}

	public void setMatrikelList(ArrayList<Stud> matrikelList) {
		this.matrikelList = matrikelList;
	}
	
	
	
}